namespace Brjnk.GpxExplorer.ViewModel
{
    using System;
    using Brjnk.Common;
    using Brjnk.GpxExplorer.Data;
    using Brjnk.MVVM;

    public class TrackSegmentVM : Wrapper<TrackSegment>, IGpsData
    {
        static TrackSegmentVM()
        {
            WrapProperty(new PropertyWrapperInfo<TrackSegment>("Points",
                     r => r.Points, null, o => new CollectionWrapper<TrackPoint, TrackPointVM>((ObservableCollectionEx<TrackPoint>)o), false));
        }

        public TrackSegmentVM(TrackSegment source)
            : base(source)
        {
        }

        public CollectionWrapper<TrackPoint, TrackPointVM> Points
        {
            get { return (CollectionWrapper<TrackPoint, TrackPointVM>)GetWrapper("Points"); }
        }

        public Distance Distance { get; internal set; }

        public DateTime? StartTime { get; internal set; }

        public DateTime? EndTime { get; internal set; }

        public int Num { get; internal set; }

        public TimeSpan? Duration
        {
            get
            {
                if (StartTime.HasValue && EndTime.HasValue)
                    return EndTime.Value - StartTime.Value;
                return null;
            }
        }

        public void GetBounds(BoundsBuilder builder)
        {
            foreach (var point in Points) builder.AddPoint(point.PositionI);
        }
    }
}